<template>
  <div class="app-container repayment-plan">
    <avue-form
      :option="queryOption"
      v-model="queryParams"
      ref="searchForm"
      v-show="showSearch"
      @keydown.enter="handleQuery"
    >
      <template #menu-form="{ size }">
        <el-button
          icon="Search"
          type="primary"
          :size="size"
          @click="handleQuery"
          >查询</el-button
        >
        <el-button icon="Refresh" :size="size" @click="handleEmpty(searchForm)"
          >重置</el-button
        >
      </template>
    </avue-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="Plus" @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          icon="EditPen"
          plain
          @click="handleUpdate"
          :disabled="disUpdate"
          v-hasPermi="['financial:repaymentPlan:update']"
          >编辑</el-button
        >
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="Delete"
          plain
          @click="handleDelete"
          :disabled="disDelete"
          >删除</el-button
        >
      </el-col>
      <!-- <right-toolbar
        @queryTable="getList"
        v-model:showSearch="showSearch"
      ></right-toolbar> -->
    </el-row>

    <el-table
      v-loading="loading"
      :data="tableList"
      row-key="id"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="30" align="center" />
      <el-table-column prop="orderTitle" label="关联订单">
        <template #default="scope">
          <el-link
            type="primary"
            :underline="false"
            disabled
            @click="goTo('url', { orderId: scope.row.orderId })"
            >{{ scope.row.orderTitle }}</el-link
          >
        </template>
      </el-table-column>
      <el-table-column prop="customerName" label="订单归属人" width="200">
      </el-table-column>
      <el-table-column prop="qc" label="回款期次" width="200">
        <template #default="scope">
          <span>第{{ scope.row.period }}期(共{{ scope.row.allPeriod }}期)</span>
        </template>
      </el-table-column>
      <el-table-column prop="price" label="计划回款金额" width="200">
        <template #default="scope">
          <span>¥{{ scope.row.price }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="repaymentPrice" label="已回款金额" width="200">
        <template #default="scope">
          <span>¥{{ scope.row.repaymentPrice }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="repaymentPlanDate"
        label="计划回款日期"
        width="200"
      >
      </el-table-column>
      <el-table-column prop="status" label="回款状态" width="100">
        <template #default="scope">
          <dict-drop
            :options="repayment_status"
            :value="scope.row.status"
          ></dict-drop>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="150"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="View"
            @click="handleDetails(scope.row)"
            >详情</el-button
          >
          <el-button
            link
            type="primary"
            icon="CaretRight"
            @click="handleRecords(scope.row)"
            >回款</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 人员弹出框下拉框 -->
    <SelectDialog
      :dialog-param="dialogParam"
      @ok="selectDialogData"
      @close="
        val => {
          dialogParam.isDialog = false
        }
      "
    ></SelectDialog>
  </div>
</template>
<script setup name="RepaymentPlan">
import { reactive, toRefs, ref } from 'vue'
import { useRouter } from 'vue-router'
import usePassData from '@/store/modules/passData'
import {
  listRepaymentPlan,
  delRepaymentPlan
} from '@/api/repaymentManage/repayment'
import useCommonStore from '@/store/modules/common'
import { getTreeNodeByParam, getPricipalId } from '@/utils/commo'
const commonStore = useCommonStore()
const props = defineProps({
  repaymentData: {
    type: Object,
    require: false,
    default: () => {
      return {}
    }
  }
})
const emit = defineEmits([])
const passData = usePassData()
const router = useRouter()
const { proxy } = getCurrentInstance()
const { repayment_status, pay_type } = proxy.useDict(
  'repayment_status',
  'pay_type'
)
const userOption = ref([])
const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10
  },
  ids: [],
  selectArr: [],
  total: 0,
  queryOption: {
    menuSpan: 5,
    submitBtn: false,
    // gutter: 30,
    emptyBtn: false,
    column: [
      {
        labelWidth: '0',
        prop: 'number',
        span: 3,
        placeholder: '订单编号或商品货号',
        type: 'input'
      },
      {
        // label: '姓名',
        labelWidth: '0',
        prop: 'status',
        span: 3,
        placeholder: '逾期状态',
        type: 'select',
        dicData: repayment_status
      },
      {
        labelWidth: '0',
        prop: 'payType',
        span: 3,
        placeholder: '付款方式',
        type: 'select',
        dicData: pay_type
      },
      {
        labelWidth: '0',
        prop: 'repaymentPlanDates',
        type: 'daterange',
        span: 3,
        format: 'YYYY-MM-DD',
        valueFormat: 'YYYY-MM-DD',
        startPlaceholder: '计划回款开始时间',
        endPlaceholder: '计划回款结束时间'
      },
      {
        labelWidth: '0',
        prop: 'orderBelongDept',
        span: 3,
        placeholder: '订单所属部门',
        type: 'tree',
        props: {
          value: 'deptId',
          label: 'deptName'
        },
        dicData: []
      },
      {
        labelWidth: '0',
        prop: '_orderBelongUser',
        span: 3,
        placeholder: '订单所属人员',
        type: 'select',
        clearable: false,
        props: {
          value: 'uuid'
        },
        dicData: userOption,
        multiple: true,
        click: ({ value, column }) => {
          dialogParam.value.type = 'orderBelongUser'
          dialogParam.value.title = '请选择项目负责人'
          dialogParam.value.isDialog = true
          dialogParam.value.multiple = true
          dialogParam.value.nodeKey = 'uuid'
          dialogParam.value.defaultCheckedKeys =
            queryParams.value._orderBelongUser
          dialogParam.value.dataType = 'person'
        }
      }
    ]
  },
  searchOption: {
    submitBtn: false,
    emptyBtn: false,
    menuPosition: 'left',
    column: [
      {
        label: '订单查询',
        // labelWidth: '0',
        prop: 'name',
        span: 12,
        placeholder: '订单号',
        type: 'input'
      },
      {
        label: '订单状态',
        // labelWidth: '0',
        span: 12,
        prop: 'sex',
        placeholder: '审批状态',
        type: 'select',
        dicData: [
          {
            label: '字典1',
            value: 0
          },
          {
            label: '字典2',
            value: 1
          }
        ]
      },
      {
        label: '提交时间',
        // labelWidth: '0',
        span: 12,
        prop: 'datetimerange',
        type: 'datetimerange',
        format: 'YYYY-MM-DD HH:mm:ss',
        valueFormat: 'YYYY-MM-DD HH:mm:ss',
        startPlaceholder: '提交开始时间',
        endPlaceholder: '提交结束时间'
      },
      {
        label: '订单类型',
        // labelWidth: '0',
        span: 12,
        prop: 'sex',
        placeholder: '订单类型',
        type: 'select',
        dicData: [
          {
            label: '字典1',
            value: 0
          },
          {
            label: '字典2',
            value: 1
          }
        ]
      },
      {
        label: '付款时间',
        // labelWidth: '0',
        span: 12,
        prop: 'datetimerange',
        type: 'datetimerange',
        format: 'YYYY-MM-DD HH:mm:ss',
        valueFormat: 'YYYY-MM-DD HH:mm:ss',
        startPlaceholder: '签订开始时间',
        endPlaceholder: '签订结束时间'
      },
      {
        label: '订单来源',
        // labelWidth: '0',
        span: 12,
        prop: 'sex',
        placeholder: '订单来源',
        type: 'select',
        dicData: [
          {
            label: '字典1',
            value: 0
          },
          {
            label: '字典2',
            value: 1
          }
        ]
      },
      {
        label: '支付方式',
        // labelWidth: '0',
        span: 12,
        prop: 'sex',
        placeholder: '支付方式',
        type: 'select',
        dicData: [
          {
            label: '字典1',
            value: 0
          },
          {
            label: '字典2',
            value: 1
          }
        ]
      },
      {
        label: '订单金额',
        // labelWidth: '0',
        span: 12,
        prop: 'sex',
        placeholder: '订单金额',
        type: 'select',
        dicData: [
          {
            label: '字典1',
            value: 0
          },
          {
            label: '字典2',
            value: 1
          }
        ]
      },
      {
        label: '发票类型',
        prop: 'radio',
        type: 'radio',
        dicData: [
          {
            label: '男',
            value: 0
          },
          {
            label: '女',
            value: 1
          },
          {
            label: '未知',
            value: ''
          }
        ]
      },
      {
        label: '发票状态',
        prop: 'radio',
        type: 'radio',
        dicData: [
          {
            label: '男',
            value: 0
          },
          {
            label: '女',
            value: 1
          },
          {
            label: '未知',
            value: ''
          }
        ]
      }
    ]
  },
  showSearch: true,
  searchDrawer: false /* 高级检索框 */,
  disUpdate: true /* 编辑按钮是否可编辑 */,
  disDelete: true /* 关闭按钮是否可编辑 */,
  loading: false,
  tableList: [],
  productOptions: [],
  dialogParam: {} /* 人员弹窗框参数 */
})
const {
  queryParams,
  ids,
  selectArr,
  total,
  queryOption,
  searchOption,
  showSearch,
  searchDrawer,
  disUpdate,
  disDelete,
  loading,
  tableList,
  productOptions,
  dialogParam
} = toRefs(data)
const selectDialogData = val => {
  switch (val.type) {
    case 'orderBelongUser':
      userOption.value = val.selectData
      // queryOption.value.column[4].dicData = val.selectData
      queryParams.value._orderBelongUser = val.defaultCheckedKeys
      break
  }
}
// 提交
function handleQuery() {
  getList()
}
// 重置
function handleEmpty(formEl) {
  proxy.$refs.searchForm.resetForm()
  getList()
}

// 初始化
async function init() {
  // 归属部门
  let response = await commonStore.getDeptTreeData()
  queryOption.value.column[4].dicData = proxy.handleTree(response, 'deptId')

  getList()
}
// 获取数据列表
function getList() {
  loading.value = true
  // 订单所属人
  queryParams.value.orderBelongUser = getPricipalId(
    userOption.value,
    queryParams.value._orderBelongUser
  )
  listRepaymentPlan(queryParams.value).then(res => {
    loading.value = false
    tableList.value = res.rows
    total.value = res.total
    console.log(' tableList.value==>', tableList.value)
  })
  console.log('获取数据')
}
/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  selectArr.value = selection
  disUpdate.value = selection.length != 1
  disDelete.value = !selection.length
}

function goTo(url, data) {
  router.push({ path: url, query: data })
}

function handleAdd() {
  goTo('/financial/repaymentPlanForm', {})
}
function handleUpdate() {
  goTo('/financial/repaymentPlanForm', { salesOrderId: ids.value[0] })
}
function handleDetails(row) {
  goTo('/financial/repaymentPlanDetails', { salesOrderId: row.id })
}
// 新增回款记录
function handleRecords(row) {
  goTo('/financial/repaymentForm', { repaymentData: JSON.stringify(row) })
}
function handleDelete(row) {
  let rowIds = row.id || ids.value
  console.log('selectArr-->', selectArr.value, 'id==>', rowIds)
  proxy.$modal
    .confirm(
      '是否确认删除名称为"' + selectArr.value.map(e => e.name) + '"的数据项?'
    )
    .then(function () {
      return delRepaymentPlan(rowIds)
    })
    .then(() => {
      getList()
      proxy.$modal.msgSuccess('删除成功')
    })
    .catch(() => {})
}

init()
</script>
<style scoped>
.yzh-indent :deep(.el-drawer__header) {
  background-color: rgba(245, 245, 245, 1);
  margin-bottom: 0;
  padding-bottom: 20px;
}
.search-drawer-retract {
  color: #999999;
}
</style>
